<?php
class FileFlag_Api
	{
		private $flag='fileflag';
		private $method='file_request';
		private $type='json';
		private $charset='utf-8';
		private $ver=1;
		private $token='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';//秘钥
		private $post_data=array();
		private $sign=null;//签名
		private $timestamp=null;//时间截
		private $data='';
		
		private $result=array(
			0=>array('code'=>'','info'=>'FAILED','msg'=>'','data'=>''),
			1=>array('code'=>'','info'=>'SUCCESS','msg'=>'','data'=>'')
		);
		
		private $key=0;//变量$result的键值 $this->result  (key)
		
		public function __construct()
			{
				
			}
			
		private function set_sign($sign=array())
			{
				$return_sign='';
				
				if(!is_array($sign))
					{
						return null;
					}
					else
						{
							ksort($sign,SORT_STRING);
							
							foreach($sign as $key=>$value)
								{
									if( $value != null && strtolower($key) != 'sign' )
										{
											if( get_magic_quotes_gpc() )
												{
													$value=stripslashes($value);
													$return_sign.=$key.(is_array($value)?$this->set_sign($value):$value);
												}
												else
													{
														$return_sign.=$key.(is_array($value)?$this->set_sign($value):$value);
													}
											//$return_sign.=$key.(is_array($value)?$this->set_sign($value):$value);
										}
										else
											{
												
											}
								}
							return $return_sign;
						}
			}
			
		private function get_sign($sign)
			{
				if($sign)
					{
						return strtoupper(md5(strtoupper(md5($sign)).$this->token));
					}
					else
						{
							return null;
						}
			}
			
		private function check_sign($sign=array())
			{
				$sign_str=$this->set_sign($sign);
				$signs=$this->get_sign($sign_str);
				if($this->sign==$signs)
					{
						return true;
					}
					else
						{
							return false;
						}
			}
			
		private function check_timestamp()
			{
				if($this->timestamp)
					{
						if(($this->timestamp+60)<time())
							{
								return true;
							}
							else
								{
									return false;
								}
					}	
			}
		
		private function check_post($post)
			{
				
				if(!empty($post))
					{
						$type=$post['type'];
						$flags=$post['flag'];
						$timestamp=$post['timestamp'];
						$this->data=$post['data'];
						$sign=$post['sign'];
						$method=$post['method'];
						$ver=$post['ver'];
						$flag=true;
						if($type!=$this->type)
							{
								$flag=false;
								$this->key=0;
								$this->result[$this->key]['code']='type';
								$this->result[$this->key]['msg']='Response:type field Invalid!';
							}
							elseif($flags!=$this->flag)
								{
									$this->key=0;
									$flag=false;
									$this->result[$this->key]['code']='flag';
									$this->result[$this->key]['msg']='Response:flag field Invalid!';
								}
								elseif($sign!=$this->sign)
									{
										$this->key=0;
										$flag=false;
										$this->result[$this->key]['code']='sign';
										$this->result[$this->key]['msg']='Response:sign field Invalid!';
									}
									elseif($method!=$this->method)
										{
											$this->key=0;
											$flag=false;
											$this->result[$this->key]['code']='method';
											$this->result[$this->key]['msg']='Response:method field Invalid!';
										}
										elseif($ver!=$this->ver)
											{
												$this->key=0;
												$flag=false;
												$this->result[$this->key]['code']='ver';
												$this->result[$this->key]['msg']='Response:ver field Invalid!';
											}
											else
												{
													$this->key=1;
												}
						
						//添加转送数据的合法性
						return $flag;
					}
					else
						{
							return false;
						}
				
			}
			
		private function PostFilter($post)
			{
				$return_post=array();
						if(is_array($post))
							{
								foreach( $post as $key => $value )
									{
										if(is_array($value) && !empty($value))
											{
												$value=$this->PostFilter($value);
											}
											else
												{
													if(!get_magic_quotes_gpc())
														{
															$value=addslashes($value);
														}
													$value=strip_tags($value);
												}
										$return_post[$key]=$value;
									}
							}
							/*  else
								{
									if(!get_magic_quotes_gpc())
										{
											$return_post[]=addslashes($post);
										}
									$post[]=strip_tags($post);
								} */
				return $return_post;
			}
			
		public function DoResponse($post=array())
			{
				$sign=$this->set_sign($post);
				$this->sign=$this->get_sign($sign);
				$check=$this->check_post($post);
				if($check)
					{
						$this->data=json_decode(trim(stripslashes(urldecode($this->data)),chr(239).chr(187).chr(191)),true);
						$post=$this->PostFilter($this->data);
						$this->post_data=$post;
						if($this->data_check())
							{
								return false;
							}
							else
								{
									return true;
								}
						//return true;
					}
					else
						{
							return false;
						}
				
			}
			
		private function data_check()
			{
				$field_array=array('order_sn','code','is_upfile','fileinfo');
				if(empty($this->post_data))
					{
						$this->key=0;
						$this->result[$this->key]['code']='data';
						$this->result[$this->key]['msg']='Response:data is empty!';
						return false;
					}
					elseif(!empty($this->post_data) && is_array($this->post_data))
						{
							
							foreach($this->post_data as $key => $value)
								{
									if(in_array(strtolower($key),$field_array))
										{
											if(is_array($value) && !empty($value))
												{
													
													/* foreach($value as $k => $v)
														{
															if(is_array($v) && !empty($v))
																{
																	foreach($v as $ky => $val)
																		{
																			if(in_array(strtolower($ky),$field_array))
																				{
																					if($ky=='code')
																						{
																							if(empty($val))
																								{
																									$this->key=0;
																									$this->result[$this->key]['code']='data';
																									$this->result[$this->key]['msg']='Response:'.$ky.' is empty!';
																									return false;
																								}
																						}
																					
																				}
																		}
																		
																}
															
														} */
												}
												elseif(empty($value) && !is_array($value))
													{
														$this->key=0;
														$this->result[$this->key]['code']='data';
														$this->result[$this->key]['msg']='Response:'.$key.' is empty!';
														return false;
													}
										}
									
								}
						}
						else
							{
								return true;
							}
				
			}
		
		public function display()
			{
				//$this->result[$this->key]['data']=$this->data;
				//$json=json_encode($this->result[$this->key]);
				//echo $json;
				$rs=new stdClass();
				foreach($this->result[$this->key] as $k => $v)
					{
						$rs->$k=$v;//$this->result[$this->key][$k];
					}
				$rs=json_encode($rs);
				echo $rs;
			}
			
		public function get_request_data()
			{
				return $this->post_data;
			}
		
	}


?>